package com.wavehi.service.impl;

import com.wavehi.mapper.UserMapper;
import com.wavehi.pojo.User;
import com.wavehi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @Author WaveHi
 * @Date 22/10/2021 下午 9:27
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean transfer(Long srcId, Long destId, BigDecimal money) {
        //Get src user
        User user = userMapper.selectById(srcId);
        //Get dest user
        User user1 = userMapper.selectById(destId);
        //Transfer
        user.setMoney(user.getMoney().subtract(money));
        user1.setMoney(user1.getMoney().add(money));
        return userMapper.update(user) && userMapper.update(user1);
    }
}
